package com.guigu.userservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.MD5;
import com.guigu.common.utils.R;
import com.guigu.userservice.entity.Businessman;
import com.guigu.userservice.entity.Operator;
import com.guigu.userservice.service.BusinessmanService;
import com.guigu.userservice.service.OperatorService;
import com.guigu.userservice.vo.OperatorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作员表 前端控制器
 * </p>
 *
 * @author zcy
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/user/operator")
public class OperatorController {

    @Autowired
    private OperatorService operatorService;
    
    @Autowired
    private BusinessmanService businessmanService;


    @PostMapping("/login/{username}/{password}")
    public R Login(@PathVariable("username") String username, @PathVariable("password") String password) {
        return operatorService.Login(username, password);
    }
    @PutMapping("/passwordChange/{username}/{oldPassword}/{newPassword}")
    public R passwordChange(@PathVariable("username") Integer username, @PathVariable("oldPassword") String oldPassword, @PathVariable("newPassword") String newPassword) {
        return operatorService.passwordChange(username, oldPassword, newPassword);
    }

    @GetMapping("/queryOperatorList")
    public R queryOperatorList() {
        List<Operator> list = operatorService.list();
        List<Businessman> list1 = businessmanService.list();

        Set<Integer> businessmanOperatorIds = list1.stream()
                .map(Businessman::getOperatorId)
                .collect(Collectors.toSet());

        List<Operator> filteredList = list.stream()
                .filter(operator -> !businessmanOperatorIds.contains(operator.getOperatorId()))
                .collect(Collectors.toList());

        return R.ok().data("list", filteredList);
    }
    @PostMapping("/queryOperator/{pageNum}/{pageSize}")
    public R queryOperator(@PathVariable("pageNum") Integer pageNum,
                           @PathVariable("pageSize") Integer pageSize,
                           @RequestBody OperatorVo vo) {
        return operatorService.queryOperatorList(pageNum, pageSize, vo);
    }
    @GetMapping("/queryOperatorById/{id}")
    public R queryOperatorById(@PathVariable("id") Integer id) {
        Operator operator = operatorService.getById(id);
        return R.ok().data("operator", operator);
    }

    @PostMapping("/addOperator")
    public R addOperator(@RequestBody Operator operator) {
        QueryWrapper<Operator> name = new QueryWrapper<>();
        name.eq("login_name", operator.getLoginName());
        Operator one = operatorService.getOne(name);
        QueryWrapper<Operator> phone = new QueryWrapper<>();
        phone.eq("phone", operator.getLoginName());
        Operator two = operatorService.getOne(phone);
        if (one != null) {
            return R.error().message("登录账号已存在");
        }
        if (two != null) {
            return R.error().message("手机号已存在");
        }
        operator.setPassword(MD5.encrypt("123456"));
        operatorService.save(operator);
        return R.ok();
    }
    @PutMapping("/updateOperator")
    public R updateOperator(@RequestBody Operator operator) {
        operator.setPassword(MD5.encrypt(operator.getPassword()));
        QueryWrapper<Operator> name = new QueryWrapper<>();
        name.eq("login_name", operator.getLoginName());
        name.ne("operator_id", operator.getOperatorId());
        Operator one = operatorService.getOne(name);
        QueryWrapper<Operator> phone = new QueryWrapper<>();
        phone.eq("phone", operator.getPhone());
        phone.ne("operator_id", operator.getOperatorId());
        Operator two = operatorService.getOne(phone);
        if (one != null) {
            return R.error().message("登录账号已存在");
        }
        if (two != null) {
            return R.error().message("手机号已存在");
        }
        operatorService.updateById(operator);
        return R.ok();
    }

    @PutMapping("/updatePassword/{id}/{password}")
    public R updatePassword(@PathVariable("id") Long id, @PathVariable("password") String password) {
        operatorService.updatePassword(id, password);
        return R.ok();
    }
}
